package com.netsdk.lht2.demo;

import cn.hutool.core.util.CharsetUtil;
import com.netsdk.demo.util.sql.Trajectory;
import com.netsdk.lht2.util.TrajectoryUtil;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class Demo {
    private final static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) throws Exception {
        LocalDateTime startTime = LocalDateTime.now();
        Socket    socket = new Socket("127.0.0.1",8091);
        while (true){
            if(socket.isConnected()){
                //  右转
                List<Trajectory[]> data1 = TrajectoryUtil.create("2024-12-10 20:00:00",1,"352","1");
                TrajectoryUtil.setTime(data1.get(0), startTime.plusSeconds(47));
                // 左转
                List<Trajectory[]> data2 = TrajectoryUtil.create("2024-12-10 20:00:00",2,"352","2");
                TrajectoryUtil.setTime(data2.get(0), startTime.plusSeconds(58));
                TrajectoryUtil.setTime(data2.get(1), startTime.plusSeconds(62));
                // 直行
                List<Trajectory[]> data3 = TrajectoryUtil.create("2024-12-10 20:00:00",15,"352","3");
                for (int i = 0; i < data3.size(); i++) {
                    TrajectoryUtil.setTime(data3.get(i), startTime.plusSeconds(2).plusSeconds(i));
                }
                List<Trajectory[]> data4 = TrajectoryUtil.create("2024-12-10 20:00:00",10,"352","4");
                for (int i = 0; i < data4.size()-1; i++) {
                    TrajectoryUtil.setTime(data4.get(i), startTime.plusSeconds(5).plusSeconds(i));
                }
                TrajectoryUtil.setTime(data4.get(data4.size()-1), startTime.plusSeconds(36));

                // 右转
//        List<Trajectory[]> data5 = TrajectoryUtil.create("2024-12-10 20:00:00",1,"352","5");
                // 排序
                List<Trajectory> data =new ArrayList<>();
                data1.forEach(el-> data.addAll(Arrays.asList(el)));
                data2.forEach(el-> data.addAll(Arrays.asList(el)));
                data3.forEach(el-> data.addAll(Arrays.asList(el)));
                data4.forEach(el-> data.addAll(Arrays.asList(el)));
                data.sort(Comparator.comparing(Trajectory::getTime));
                StringBuilder sb = new StringBuilder();
                for (Trajectory trajectory : data) {
                    String body = TrajectoryUtil.toPushString(trajectory);
                    sb.append(body);
                }
                System.out.println(new Date() + sb.toString());
                OutputStream outputStream = socket.getOutputStream();
                sb.append("\n");
                String dataStr = sb.toString();
                outputStream.write(dataStr.getBytes(StandardCharsets.UTF_8));
                System.out.println("发送成功："+ new Date());
                Thread.sleep(120*1000);
                startTime = startTime.plusSeconds(120);
            }
        }
    }
}
